package com.ruoyi.manage.cms.controller;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.utils.DateUtil;
import com.ruoyi.common.utils.ObjUtil;
import com.ruoyi.manage.cms.domain.HbNews;
import com.ruoyi.manage.cms.domain.vo.HbNewsVo;
import com.ruoyi.manage.cms.service.IHbNewsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;

import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StrUtil;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.poi.ExcelUtil;
/**
 * newsController
 *
 * @author ruoyi
 * @date 2025-07-18
 */
@Api(tags="news控制器")
@RestController
@RequestMapping("/manage/news" )
public class HbNewsController extends BaseController {
    /**
    * 配置表别名 默认空
    */
    private final static String TABLE_PREFIX=StrUtil.initTableAlias(null);
    @Autowired
    private IHbNewsService hbNewsService;

    /**
     * 查询news列表
     */
    @ApiOperation(value = "查询news",notes = "news管理")
    @PreAuthorize("@ss.hasPermi('manage:news:list')" )
    @GetMapping("/list" )
    public TableDataInfo list(HbNewsVo hbNews, QueryWrapper<HbNews> queryWrapper) {
        setQueryCondition(hbNews,queryWrapper);
        startPage();
        List<HbNews> list = hbNewsService.list(queryWrapper);
        return getDataTable(list);
    }
    private void setQueryCondition(HbNewsVo hbNews, QueryWrapper<HbNews> queryWrapper) {
        if(StrUtil.isNotBlank(hbNews.getTitle())){
            queryWrapper.like(TABLE_PREFIX+"title" ,hbNews.getTitle());
        }
        if(StrUtil.isNotBlank(hbNews.getSource())){
            queryWrapper.like(TABLE_PREFIX+"source" ,hbNews.getSource());
        }
        if(hbNews.getIsTop() != null){
            queryWrapper.eq(TABLE_PREFIX+"is_top" ,hbNews.getIsTop());
        }
        if(hbNews.getStatus() != null){
            queryWrapper.eq(TABLE_PREFIX+"status" ,hbNews.getStatus());
        }
        if (ObjUtil.isNotNull(hbNews.getBeginPublishTime()) && ObjUtil.isNotNull(hbNews.getEndPublishTime())){
            queryWrapper.between(TABLE_PREFIX+"publish_time", DateUtil.beginOfDay(hbNews.getBeginPublishTime()),DateUtil.endOfDay(hbNews.getEndPublishTime()));
        }
        queryWrapper.orderByDesc(TABLE_PREFIX+"create_time");
    }

    /**
     * 导出news列表
     */
    @PreAuthorize("@ss.hasPermi('manage:news:export')" )
    @PostMapping("/export" )
    public void export(HttpServletResponse response, HbNewsVo hbNews,QueryWrapper<HbNews> queryWrapper) {
        if (CollUtil.isEmpty( hbNews.getIds())){
            setQueryCondition(hbNews,queryWrapper);
        }else {
            queryWrapper.in(TABLE_PREFIX+"id",hbNews.getIds());
        }
        List<HbNews> list = hbNewsService.list(queryWrapper);
        ExcelUtil<HbNews> util = new ExcelUtil<HbNews>(HbNews. class);
        util.exportExcel(response,list, "news");
    }

    /**
     * 获取news详细信息
     */
    @ApiOperation(value = "获取news",notes = "news管理")
    @PreAuthorize("@ss.hasPermi('manage:news:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) String id) {
        return AjaxResult.success(hbNewsService.queryGetById(id));
    }

    /**
     * 新增news
     */
    @ApiOperation(value = "新增news",notes = "news管理")
    @PreAuthorize("@ss.hasPermi('manage:news:add')" )
    @RepeatSubmit
    @PostMapping
    public AjaxResult add(@Validated @RequestBody HbNews hbNews) {
        return toAjax(hbNewsService.addHbNews(hbNews) ? 1 : 0);
    }

    /**
     * 修改news
     */
    @ApiOperation(value = "修改news",notes = "news管理")
    @PreAuthorize("@ss.hasPermi('manage:news:edit')" )
    @PutMapping
    public AjaxResult edit(@RequestBody HbNews hbNews) {
        return toAjax(hbNewsService.updateHbNews(hbNews) ? 1 : 0);
    }

    /**
     * 删除news
     */
    @ApiOperation(value = "删除news",notes = "news管理")
    @PreAuthorize("@ss.hasPermi('manage:news:remove')" )
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(hbNewsService.deleteHbNewsByIds(ids) ? 1 : 0);
    }
}
